from django.shortcuts import render_to_response
from django.http import HttpResponse
from settings import BASE_DIR
import json
import os
from lxml import etree


def home(request):
    return render_to_response('home/home.html')


def data(request):
    return render_to_response('data/data.html', {'dataClass': 'active'})


def about(request):
    return render_to_response('about/about.html', {'aboutClass': 'active'})


def parseJsonDataRequest(request):
    organization = request.GET.get('organization')
    interval =  request.GET.getlist('interval[]')
    return organization, interval[0], interval[1]

    #<aid xmlns="http://www.fi.muni.cz/internationalaid" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fi.muni.cz/internationalaid aid.xsd">

def jsonData(request):
    organization, firstYear, lastYear = parseJsonDataRequest(request)
    tree = etree.parse(os.path.join(BASE_DIR,'static/xml/data.xml'))
    nstree = {'aid':'http://www.fi.muni.cz/internationalaid'}
    country = tree.xpath('//aid:country', namespaces=nstree)
    data = {"interval" : [int(firstYear),int(lastYear)]}
    countries = {}
    if organization == "UNDP" :
        data["TWB"] = False
        data["UNDP"] = True
        for element in country:
            org = {}
            twb = {}
            undp = {}
            twb["sum"] = 0
            for i in range(int(firstYear),int(lastYear)+1):
                twb[str(i)] = 0
            undp["sum"] = int(element.xpath('sum(aid:transaction[@organization="UNDP"])', namespaces=nstree))
            for i in range(int(firstYear),int(lastYear)+1):
                undp[str(i)] = int(element.xpath('sum(aid:transaction[@organization="UNDP" and @year = ' + str(i) + '])', namespaces=nstree))
            org["TWB"] = twb
            org["UNDP"] = undp
            countries[element.attrib['code']] = org
    if organization == "TWB" :
        data["TWB"] = True
        data["UNDP"] = False
        for element in country:
            org = {}
            twb = {}
            undp = {}
            twb["sum"] = int(element.xpath('sum(aid:transaction[@organization="TWB"])', namespaces=nstree))
            for i in range(int(firstYear),int(lastYear)+1):
                twb[str(i)] = int(element.xpath('sum(aid:transaction[@organization="TWB" and @year="' + str(i) + '"])', namespaces=nstree))
            undp["sum"] = 0
            for i in range(int(firstYear),int(lastYear)+1):
                undp[str(i)] = 0
            org["TWB"] = twb
            org["UNDP"] = undp
            countries[element.attrib['code']] = org
    if organization == "TWB+UNDP" :
        data["TWB"] = True
        data["UNDP"] = True
        for element in country:
            org = {}
            twb = {}
            undp = {}
            twb["sum"] = int(element.xpath('sum(aid:transaction[@organization="TWB"])', namespaces=nstree))
            for i in range(int(firstYear),int(lastYear)+1):
                twb[str(i)] = int(element.xpath('sum(aid:transaction[@organization="TWB" and @year = ' + str(i) + '])', namespaces=nstree))
            undp["sum"] = int(element.xpath('sum(aid:transaction[@organization="UNDP"])', namespaces=nstree))
            for i in range(int(firstYear),int(lastYear)+1):
                undp[str(i)] = int(element.xpath('sum(aid:transaction[@organization="UNDP" and @year = ' + str(i) + '])', namespaces=nstree))
            org["TWB"] = twb
            org["UNDP"] = undp
            countries[element.attrib['code']] = org
    data["countries"] = countries
    result = json.dumps(data, indent=1)
    #f = open("blahblah", "w")
    #print >> f, result
    return HttpResponse(result, mimetype='application/json')

